import 'dart:math';

import 'package:flutter/cupertino.dart';

import '../bean/bean.dart';

class MyPainter extends CustomPainter {
  List<BobbleBean> list;
  Random random;
  Paint _paint = new Paint()..isAntiAlias = true;
  MyPainter({required this.list, required this.random});
  @override
  void paint(Canvas canvas, Size size) {
    list.forEach((element) {
      Offset newConterOffset = calculateXY(element.speed, element.theta);
      double dx = newConterOffset.dx + element.offset.dx;
      double dy = newConterOffset.dy + element.offset.dy;
      if (dx < 0 || dx > size.width) {
        dx = random.nextDouble() * size.width;
      }
      if (dy < 0 || dy > size.height) {
        dy = random.nextDouble() * size.height;
      }
      element.offset = Offset(dx, dy);
    });
    list.forEach((element) {
      _paint.color = element.color;
      canvas.drawCircle(element.offset, element.radius, _paint);
    });
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }

  Offset calculateXY(double speed, double theta) {
    return Offset(speed * cos(theta), speed * sin(theta));
  }
}
